//引入db配置
const db = require('../config/db')
//引入sequelize对象
const Sequelize = db.sequelize

//引入数据表模型
const user = Sequelize.import('../module/user');
//自动创建表
user.sync({
    force: false
});

//数据库操作类
class userModule {
    static async userRegist(data) {
        console.log(`userRegist start ${JSON.stringify(data)}`)
        return await user.create({
            first_name: data.first_name,
            last_name: data.last_name,
            email: data.email,
            phone: data.phone,
            business_name: data.business_name,
            dba_name: data.dba_name,
            unionpay: data.unionpay,
            alipay: data.alipay,
            wechat_payments: data.wechat_payments,
            terms_conditions: data.terms_conditions,
            avg_ticket: data.avg_ticket,
            max_ticket: data.max_ticket,
            client_ip: data.client_ip,
            website: data.website
        })
    }
}
//功能处理
class userController {
    // 创建新的application
    static async create(ctx) {
        const req = ctx.request.body; // 直接打印ctx的话是在request里面看不到body信息的
        console.log(`req: ${JSON.stringify(req)}`)
        // todo:
        // 1. 人机交互验证 服务端请求谷歌
        // 2. 表单权限 jwt 权限
        try {
            console.log(`start`)
            const param = {
                first_name: req.first_name,
                last_name: req.last_name,
                email: req.email,
                phone: req.phone,
                business_name: req.business_name,
                dba_name: req.dba_name,
                unionpay: req.unionpay,
                alipay: req.alipay,
                wechat_payments: req.wechat_payments,
                terms_conditions: req.terms_conditions,
                avg_ticket: req.avg_ticket,
                max_ticket: req.max_ticket,
                client_ip: req.client_ip,
                website: req.website
            }
            console.log(`ing`)
            const data = await userModule.userRegist(param); 
            console.log(`end`)
            console.log(`data: ${JSON.stringify(data)}`)
            ctx.response.status = 200;
            ctx.body = {
                code: 0,
                desc: '添加成功',
                data
            }
        } catch (error) {
            ctx.response.status = 416;
            ctx.body = {
                code: -1,
                desc: '添加失败'
            }
        }
    }
}

module.exports = userController;